package com.huawei.spare.part.replace.ssh;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.LoggerFactory;
import net.schmizz.sshj.common.StreamCopier;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

import java.io.Console;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/** This examples demonstrates how a remote command can be executed. */
public class Exec {
    private static final Console con = System.console();

    public static void main(String... args)
            throws IOException {
        final SSHClient ssh = new SSHClient();
        ssh.addHostKeyVerifier(new PromiscuousVerifier());
//        ssh.loadKnownHosts();
        ssh.connect("112.93.129.107");
        ssh.authPassword("root", "Admin@7*24");
//        ssh.connect("115.159.160.190");
//        ssh.authPassword("root", "!QAZ2wsx#EDC");
        Session session = null;
        try {
//            ssh.authPublickey(System.getProperty("user.name"));
            session = ssh.startSession();
            final Command cmd = session.exec("smmget -l swi1 -d presence");

            new StreamCopier(cmd.getInputStream(), System.out, LoggerFactory.DEFAULT).spawn("stdout");
            new StreamCopier(cmd.getErrorStream(), System.err, LoggerFactory.DEFAULT).spawn("stderr");

            new StreamCopier(System.in, cmd.getOutputStream(), LoggerFactory.DEFAULT)
                    .copy();

            // Wait for session & X11 channel to get closed
            cmd.join();
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (IOException e) {
                // Do Nothing   
            }
            
            ssh.disconnect();
        }
    }

}
